#include "Character.h"


Character::Character() 
	: name("Default")
	, side("ally")
	, id("default")
	, isAlive(true)
	, SDLAnimation()
{
	XMLReader xml = XMLReader();
	std::stringstream ss;
	ss << "game/" << side << "/" << id << "/value";

	tinyxml2::XMLElement* thiselement = xml.GetElement(ss.str());

	stats[HP] = xml.GetValueFromElement(thiselement, "hp");
	stats[MP] = xml.GetValueFromElement(thiselement, "mp");
	stats[STR] = xml.GetValueFromElement(thiselement, "str");
	stats[INT] = xml.GetValueFromElement(thiselement, "int");
	maxStats[HP] = xml.GetValueFromElement(thiselement, "hp");
	maxStats[MP] = xml.GetValueFromElement(thiselement, "mp");
}

Character::Character(std::string name, std::string side, std::string id)
	: name(name)
	, side(side)
	, isAlive(true)
{
	XMLReader xml = XMLReader();
	std::stringstream ss;
	ss << "game/" << side << "/" << id << "/value";

	tinyxml2::XMLElement* thiselement = xml.GetElement(ss.str());

	stats[HP] = xml.GetValueFromElement(thiselement, "hp");
	stats[MP] = xml.GetValueFromElement(thiselement, "mp");
	stats[STR] = xml.GetValueFromElement(thiselement, "str");
	stats[INT] = xml.GetValueFromElement(thiselement, "int");
	maxStats[HP] = xml.GetValueFromElement(thiselement, "hp");
	maxStats[MP] = xml.GetValueFromElement(thiselement, "mp");
}

Character::~Character()
{

}

/*
ScreenMenu* Character::GetSpells(int sort)
{
	if(spellMenu != nullptr)
	{
		delete spellMenu;
	}

	std::vector<std::string> cursort = sort0;

	switch(sort)
	{
	case 0:
		{
			cursort = sort0;
			break;
		}
	default:
		{
			break;
		}
	}

	int count = 0;

	spellMenu = new ScreenMenu(60, 380, 110, 150, 1, );

	for(int i = 0; i < cursort.size(); i++)
	{
		spellMenu->AddMenu(SpellList::GetInstance()->getSpell(spells[i])->GetName(), true, false);
	}

	spellMenu->SetVisible(false);

	return spellMenu;
}
*/

void Character::HealHP(int value)
{
	if(isAlive)
	{
		if(stats[HP] + value > maxStats[HP])
		{
			stats[HP] = maxStats[HP];
		}
		else
		{
			stats[HP] += value;
		}
	}
}

void Character::HealMP(int value)
{
	if(isAlive)
	{
		if(stats[MP] + value > maxStats[MP])
		{
			stats[MP] = maxStats[MP];
		}
		else
		{
			stats[MP] += value;
		}
	}
}

void Character::Damage(int value)
{
	if(isAlive)
	{
		if(stats[HP] - value < 0)
		{
			stats[HP] = 0;
		}
		else
		{
			stats[HP] -= value;
		}

		if(stats[HP] == 0)
		{
			isAlive = false;
		}
	}
}


bool Character::Revive(int percent)
{
	bool ok = true;

	if(!isAlive)
	{
		isAlive = true;
		stats[HP] = maxStats[HP] / (100 / percent);
	}
	else
	{
		ok = false;
	}

	return ok;
}
